<?php

/**
 * @Author: king
 * @Date:   2019-06-05 21:42:59
 * @Last Modified by:   king
 * @Last Modified time: 2020-08-07 20:46:26
 */

namespace App\Http\Controllers\Api\v3;

use App\Models\Api\AstroSettingUser;
use App\Models\Api\Recode;
use App\Tools\Astrolog\Astrolog;
use App\Models\Api\LanmeiCounselorProblemRecode;
use Illuminate\Http\Request;
use App\Http\Controllers\Api\BaseController;


/**
 * 星盘
 * Class Astrolabe
 * @package use App\Tools\Astrolog\Astrolog
 */
class AstrolabeController extends BaseController
{
    /**
     * 查看星盘
     * @param string    chart   需要请求的星盘
     * @param int       tid     星盘类型 [1-3] 现代 古典 特殊
     * @return response\Json
     * @throws ApiException
     */
    public function index(Request $request)
    {
        if (!$request->isMethod('get')) {
            return show(config('code.error'), '未知操作', [], 403);
        }

        $param = $request->all();
        /**
         * 参数验证
         */
        $check = $this->checkParam($param);
        if ($check !== true)
        {
            return show(config('code.error'), $check);
        }
        // 星盘配置方案使用默认值
        if (isset($param['tid']) && in_array($param['tid'], [1, 2, 3]))
        {
            $tid = $param['tid'];
        } else
        {
            $tid = 1;
        }

        $settingData = (new AstroSettingUser())->getUserSettingByTid(['uid' => $request->uid, 'tid' => $tid, 'chart' => $param['chart']]);
        $Astrolog = Astrolog::getInstance();

        if (!empty($settingData))
        {
            $userSetting = [$param['chart'] => $settingData];
        } else
        {
            $userSetting = config('astroset.setting1');
        }

        if (isset($param['id']) || isset($param['id1']) || isset($param['id2']))
        {
            $Recode = new Recode();

            if (isset($param['id']) && $param['id'])
            {
                $recode = $Recode->getRecode(['id' => $param['id'], 'uid' => $request->uid]);

                $Astrolog->setRecode1($recode);
            }

            if (isset($param['id1']) && $param['id1'])
            {
                $recode = $Recode->getRecode(['id' => $param['id1'], 'uid' => $request->uid]);

                $Astrolog->setRecode1($recode);
            }

            if (isset($param['id2']) && $param['id2'])
            {
                $recode = $Recode->getRecode(['id' => $param['id2'], 'uid' => $request->uid]);

                $Astrolog->setRecode2($recode);
            }
        }

       if (isset($param['datetime']) && $param['datetime'] != '' && (strlen($param['datetime']) === 16 || strlen($param['datetime']) === 19))
        {
            $advanceTime = strtotime($param['datetime']);


            $advanceInfo = [
                'year' => date('Y', $advanceTime) * 1,
                'month' => date('m', $advanceTime) * 1,
                'day' => date('d', $advanceTime) * 1,
                'hour' => date('H', $advanceTime) * 1,
                'minute' => date('i', $advanceTime) * 1,
                'timezone' => $param['timezone'] * 1,
                'is_summer' => 0,
            ];
            if (isset($param['longitude']))
            {
                $advanceInfo['longitude'] = $param['longitude'];
            }
            if (isset($param['latitude']))
            {
                $advanceInfo['latitude'] = $param['latitude'];
            }

            $Astrolog->setAdvance($advanceInfo);
        }

        $data = $Astrolog->setExecPrefix(config('astrolog.prefix'))
            ->setIsAstrolabe(true)
            ->setUserSetting($userSetting)
            ->setGetDatas([$param['chart']])
            ->getAstroDatas()
            ->getDatas($param['chart']);

        return show(200, '加载成功', $data);
    }

    /**
     * 验证客户端传递的参数是否正确
     * @param array $param
     * @return bool|string
     */
    private function checkParam($param = [])
    {
        if (!isset($param['chart']) || !in_array($param['chart'], config('astroset.chart')))
        {
            return '未知星盘';
        }
        // 带档案id的盘
        $single = ['natal', 'transits', 'thirdprogressed', 'thirdprogressedvsnatal', 'progressions', 'progressionsvsnatal', 'solar', 'solar_return', 'solar_returnvsnatal', 'lunar_return', 'lunar_returnvsnatal', 'firdaria', 'profection'];
        // 档案id1 和 档案id2 的盘
        $double = ['composite_thirdprogressed', 'synastry', 'composite_progressions', 'davison', 'marks', 'synastry_thirdprogressed', 'synastry_progressions', 'davison_thirdprogressed', 'davison_progressions',];
        // 带推运时间 datetime 和 时区timezone 的盘
        $advance = ['now', 'transits', 'thirdprogressed', 'thirdprogressedvsnatal', 'progressions', 'progressionsvsnatal', 'solar', 'solar_return', 'solar_returnvsnatal', 'lunar_return', 'lunar_returnvsnatal', 'firdaria', 'profection', 'composite_thirdprogressed', 'composite_progressions', 'synastry_thirdprogressed', 'synastry_progressions', 'davison_thirdprogressed', 'davison_progressions',];

        $divination = ['divination'];

        // 天象盘验证
        if ($param['chart'] === 'now' && (!isset($param['longitude']) || !isset($param['latitude'])))
        {
            return '参数错误';
        }
        // 单盘验证
        if (in_array($param['chart'], $single))
        {
            if (!isset($param['id']) || $param['id'] <= 0)
            {
                return '档案错误';
            }
        }
        // 双盘验证
        if (in_array($param['chart'], $double))
        {
            if (!isset($param['id1']) || $param['id1'] <= 0 || !isset($param['id2']) || $param['id2'] <= 0)
            {
                return '档案错误';
            }
        }
        // 推进盘验证
        if (in_array($param['chart'], $advance))
        {
            if (!isset($param['datetime']) || strtotime($param['datetime']) === false)
            {
                return '时间错误';
            }

            if (!isset($param['timezone']) || $param['timezone'] > 12 || $param['timezone'] < -12)
            {
                return '时区错误';
            }
        }
        // 卜卦盘验证
        if (in_array($param['chart'], $divination))
        {
            if (!isset($param['did']) || $param['did'] <= 0)
            {
                return '未找到该卜卦';
            }
        }

        return true;
    }
}
